package org.apple.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class FileUtil {
	
	private static final String SEPERATOR = java.io.File.separator;
	
	public static void main(String[] args){
		String[] cancledList = null;
		copyFileFromDir("F:\\eclipse3.4\\workspace\\Log4j\\src\\main\\java\\org\\apache\\","F:\\eclipse3.4\\workspace\\Log4j\\tests\\src\\java\\org\\apache\\",cancledList,false);
	}

	public static void clearDir(String dir){
		File f = new File(dir);
		File[] ff = f.listFiles();
		if(ff!=null){
			for(int i = 0;i<ff.length;i++){
				File fff = ff[i];
				
				if(fff.delete())
					System.out.println("deleted file"+fff.getAbsolutePath());
				else
					System.out.println("failed to delete file"+fff.getAbsolutePath());
			}
		}else{
			System.out.println("ff is null, this is output from class FileUtil");
			return;
		}
	} 

	public static void copyFileFromDir(String targetDir, String path,boolean deleteSrc) {
		System.out.println("copying file from:"+path+" to:"+targetDir);
		File file = new File(path);
		createFile(targetDir, false);
		if (file.isDirectory()) {
			copyFileToDir(targetDir, listFile(file),deleteSrc);
		}
	}

	public static void copyFileFromDir (String targetDir, String path,ArrayList cancledList,boolean deleteSrc) {
		if(cancledList!=null){
			for(int i = 0;i<cancledList.size();i++){
				deleteFile(path+ SEPERATOR +(String)cancledList.get(i));
			}
		}
		copyFileFromDir(targetDir, path, deleteSrc);
	}

	public static void copyFileFromDir (String targetDir, String path,String[] cancledList,boolean deleteSrc) {
		if(cancledList!=null){
			for(int i = 0;i<cancledList.length;i++){
				deleteFile(path+ SEPERATOR +cancledList[i]);
			}
		}
		copyFileFromDir(targetDir, path, deleteSrc);
	}

	public static void copyFileOnly(String targetDir, String path) {
		File file = new File(path);
		File targetFile = new File(targetDir);
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (int i = 0;i<files.length;i++) {
				File subFile = files[i];
				if (subFile.isFile()) {
					copyFile(targetFile, subFile, true);
				}
			}
		}
	}

	public static void copyDir(String targetDir, String path,boolean deleteSrc) {
		File targetFile = new File(targetDir);
		System.out.println(targetFile.getAbsolutePath());
		createFile(targetFile, false);
		File file = new File(path);
		if (targetFile.isDirectory() && file.isDirectory()) {
			copyFileToDir(targetFile.getAbsolutePath() + SEPERATOR + file.getName(),
					listFile(file),deleteSrc);
		}
	}

	public static void copyFileToDir(String targetDir, String[] filePath,boolean deleteSrc) {
		if (targetDir == null || "".equals(targetDir)) {
			System.err.println("Illegal arguments, the target dir cannot be empty!");
			return;
		}
		File targetFile = new File(targetDir);
		if (!targetFile.exists()) {
			targetFile.mkdir();
		} else {
			if (!targetFile.isDirectory()) {
				System.err.println("Illegal arguments, the target argument is not a directory!");
				return;
			}
		}
		for (int i = 0;i<filePath.length;i++) {
			String path = filePath[i];
			File file = new File(path);
			if (file.isDirectory()) {
				copyFileToDir(targetDir + SEPERATOR + file.getName(), listFile(file),deleteSrc);
			} else {
				copyFileToDir(targetDir, file, "",deleteSrc);
			}
		}
	}
	public static void copyFileToDir(String targetDir,String filePath,String newName, boolean deleteSrc){
		copyFileToDir(targetDir, new File(filePath), newName,deleteSrc);
	}
	public static void copyFileToDir(String targetDir, File file, String newName,boolean deleteSrc) {
		String newFile = "";
		if (newName != null && !"".equals(newName)) {
			newFile = targetDir + SEPERATOR + newName;
		} else {
			newFile = targetDir + SEPERATOR + file.getName();
		}
		File tFile = new File(newFile);
		//System.out.println("copying file from "+file.getAbsolutePath()+"to "+newFile);
		copyFile(tFile, file, true);
		//System.out.println("copying file finished,starting to delete file...");
		if(deleteSrc)
			deleteFile(file);
		//System.out.println("delete file finished");
		
	}
	public static void deleteFile(String file){
		deleteFile(new File(file));
	}
	public static void deleteRAS(String file){
		deleteRAS(new File(file));
	}
	
	public static void deleteRAS(File file){
		if(file==null){
			return;
		}else{
			if(!file.exists()){
				System.out.println("file:"+file.getAbsolutePath()+"does not exist");
				return;
			}else{
				if(file.isDirectory()){
					File[] files = file.listFiles();
					if(files.length==0){
						deleteFile(file);
					}else if(files.length==1){
						String name = files[0].getName();
						if(name.endsWith(".ras")){
							System.out.println("starting to delete ras file:"+files[0].getAbsolutePath());
							deleteFile(files[0]);
							System.out.println("finish deleting ras file:"+files[0].getAbsolutePath());
							System.out.println("starting to delete ras file:"+file.getAbsolutePath());
							deleteFile(file);
							System.out.println("finish deleting ras file:"+file.getAbsolutePath());
							return;
						}else{
							System.out.println("there is no res file under the directory:"+file.getAbsolutePath());
							return;
						}
					}else{
						System.out.println("there are not only one ras file under the directory:"+file.getAbsolutePath());
						return;
					}
				}else{
					System.out.println("file:"+file.getAbsolutePath()+"is not a directory");
					return;
				}
			}
		}
	}
	
	public static void deleteFile(File file){
		if(file==null){
			return;
		}else{
			if(!file.exists()){
				return;
			}else{
				if(file.isDirectory()){
					File[] list = file.listFiles();
					for(int i = 0;i<list.length;i++){
						deleteFile(list[i]);
					}
				}
				System.out.println("delteting file:"+file.getPath());
				file.delete();
			}
		}
	}

	
	public static void copyFile(File targetFile, File file, boolean override) {
		if (targetFile.exists()) {
			if(override){
				System.out.println("Is going to override " + targetFile.getAbsolutePath());
				deleteFile(targetFile);
			}else{
				System.out.println("文件" + targetFile.getAbsolutePath()
						+ "is existed, is goning to skip it.");
				return;
			}
			
		}
		
		createFile(targetFile, true);
		
		System.out.println("Copying file " + file.getAbsolutePath() + " to "
				+ targetFile.getAbsolutePath());
		InputStream is = null;
		FileOutputStream fos  = null;
		try {
			is = new FileInputStream(file);
			fos = new FileOutputStream(targetFile);
			byte[] buffer = new byte[1024];
			int bytes = -1;
			while ((bytes = is.read(buffer)) != -1) {
				fos.write(buffer,0, bytes);
			}
			//fos.write(buffer);
			fos.flush();
			is.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(fos!=null){
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public static String[] listFile(File dir) {
		String absolutPath = dir.getAbsolutePath();
		String[] paths = dir.list();
		String[] files = new String[paths.length];
		for (int i = 0; i < paths.length; i++) {
			files[i] = absolutPath + SEPERATOR + paths[i];
		}
		return files;
	}
	
	public static void createFile(String path, boolean isFile){
		createFile(new File(path), isFile);
	}

	public static void createFile(File file, boolean isFile) {
		if (!file.exists()) {
			if (!file.getParentFile().exists()) {
				createFile(file.getParentFile(), false);
			} else {
				if (isFile) {
					try {
						file.createNewFile();
					} catch (IOException e) {
						e.printStackTrace();
					}
				} else {
					file.mkdir();
				}
			}
		}
	}

}